package com.lecture.java8.gui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Locale;

public class Time extends JFrame {
    private String correctUser = "1";
    private String correctPass = "1";
    private JTextField text;
    private JPasswordField password;
    private JCheckBox checkBox;

    public static void main(String[] args) {
        Time user = new Time();
        Locale.setDefault(Locale.ENGLISH);
    }

    public Time() {
        checkBox = new JCheckBox("Show");
        checkBox.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    password.setEchoChar((char) 0);
                } else {
                    password.setEchoChar('•');
                }
            }
        });

        JLabel userLabel = new JLabel("Username:");
        JLabel passLabel = new JLabel("Password:");
        JButton confirmButton = new JButton("Confirm");
        JButton cancelButton = new JButton("Cancel");

        text = new JTextField(15);
        password = new JPasswordField(15);

        JPanel userPanel = new JPanel();
        userPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        userPanel.add(new JLabel("                  "));
        userPanel.add(userLabel);
        userPanel.add(text);

        JPanel passPanel = new JPanel();
        passPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        passPanel.add(new JLabel("      "));
        passPanel.add(passLabel);
        passPanel.add(password);
        passPanel.add(checkBox);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        buttonPanel.add(confirmButton);
        buttonPanel.add(new JLabel("    "));
        buttonPanel.add(cancelButton);

        this.setLayout(new GridLayout(5, 1));
        this.add(new JLabel());
        this.add(userPanel);
        this.add(passPanel);
        this.add(buttonPanel);
        this.add(new JPanel()); // 添加一个空面板来占位

        confirmButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int a = handleLogin();
                if (a == 0) {
                    setVisible(false);
                    EventQueue.invokeLater(new Runnable() {
                        public void run() {
                            try {
                                UserListView frame = new UserListView();
                                frame.setVisible(true);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });

                }
            }
        });

        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                handleCancel();
            }
        });

        this.setTitle("Login");
        this.setSize(400, 300); // 像素作为单位
        this.setLocationRelativeTo(null); // 居中显示
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        this.setVisible(true);
    }

    private int handleLogin() {
        String currentUser = text.getText();
        String currentPass = new String(password.getPassword());
        int n = 1;
        if (currentUser.equals(correctUser) && currentPass.equals(correctPass)) {
            Object[] options = {"Enter", "Cancel"};
            n = JOptionPane.showOptionDialog(this, "Login Successful! Enter or not?", "Login Result", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options);

        } else {
            JOptionPane.showMessageDialog(this, "Login Failed", "Error", JOptionPane.ERROR_MESSAGE);
        }
        return n;
    }

    private void handleCancel() {
        text.setText("");
        password.setText("");

    }

}